package com.dwms.controller;

import com.dwms.entity.Result;
import com.dwms.enums.ResultCode;
import com.dwms.plugin.FilePlugin;
import com.dwms.plugin.FilePluginService;
import com.dwms.utils.CommonUtils;
import com.google.common.collect.Maps;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author WH
 * @className: FileController
 * @description: 文件上传
 * @create 2024/12/19 0:38
 **/
@Tag(name = "文件上传")
@Slf4j
@RestController
@RequestMapping("/file")
public class FileController {
    private final static Integer FILE_UPLOAD_MAX_SIZE = 10485760;

    @Autowired
    private FilePluginService filePluginService;

    /**
     * @methodDesc: 上传文件
     **/
    @Operation(summary = "上传文件", description = "上传文件, url地址从data里取,filePath")
    @PostMapping("/upload")
    public Result uploadFile(@RequestParam(name = "file") MultipartFile file, @RequestParam(name = "fileType") FilePlugin.FileType fileType, @RequestParam(name = "type") FilePlugin.Type type, @RequestParam(name = "useOrgFileName", defaultValue = "false", required = false) Boolean useOrgFileName) throws IOException {
        if (file == null) {
            return Result.failed(ResultCode.PARAM_IS_BLANK, "上传的文件不能为空");
        }
        if (CommonUtils.isImage(file.getInputStream()) && file.getSize() > FILE_UPLOAD_MAX_SIZE) {
            return Result.failed(ResultCode.PARAM_IS_INVALID, "上传的文件不能超过10M");
        }
        if (fileType == null || type == null) {
            return Result.failed(ResultCode.PARAM_IS_BLANK, "上传类型不能为空");
        }
        Map map = Maps.newHashMap();
        String filePath = filePluginService.uploadFile(fileType, type, file.getOriginalFilename(), useOrgFileName, file.getInputStream());
        map.put("filePath", filePath);
        return Result.success(map);
    }

    /**
     * @methodDesc: 上传批量文件
     **/
    @Operation(summary = "上传批量文件", description = "上传批量文件, 返回URL列表")
    @PostMapping("/uploadBatch")
    public Result uploadBatchFile(@RequestParam(name = "file") MultipartFile[] file, @RequestParam(name = "fileType") FilePlugin.FileType fileType, @RequestParam(name = "type") FilePlugin.Type type, @RequestParam(name = "useOrgFileName", defaultValue = "false", required = false) Boolean useOrgFileName) throws IOException {
        if (file == null) {
            return Result.failed(ResultCode.PARAM_IS_BLANK, "上传的文件不能为空");
        }
        List<String> fileList = new ArrayList<>();
        for (MultipartFile multipartFile : file) {
            String filePath = filePluginService.uploadFile(fileType, type, multipartFile.getOriginalFilename(), useOrgFileName, multipartFile.getInputStream());
            fileList.add(filePath);
        }
        return Result.success(fileList);
    }

    @Operation(summary = "文件下载", description = "文件下载")
    @RequestMapping("/download")
    public void download(String filePath, String fileName, HttpServletResponse response) {
        if (StringUtils.isNotBlank(filePath) && CommonUtils.isFileName(filePath)) {
            this.filePluginService.download(filePath, response);
        }
    }

}
